package com.douqu.game.core.entity.ext.data.boon;

import com.bean.core.buffer.ByteBuffer;
import com.bean.core.util.TimeUtils;
import com.douqu.game.core.config.FunctionConfig;
import com.douqu.game.core.config.OnlineRewardConfig;
import com.douqu.game.core.entity.Player;
import com.douqu.game.core.entity.ext.data.BaseData;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.factory.FunctionFactory;
import com.douqu.game.core.util.LogUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 在线奖励
 */
public class OnlineRewardData extends BaseData {

    //已领取的在线奖励集合
    private ArrayList<Integer> onlineRewardList;
    //当前登录开始累计时间
    private long beginTime;
    //总在线时间
    private long AllOnlineTime;

    private Player player;

    public OnlineRewardData(Player player){
        this.player = player;
        this.beginTime =0;
        this.AllOnlineTime = 0;
        this.onlineRewardList = new ArrayList<>();
    }


    /**
     * 初始化每日在线奖励
     * @param type 0 玩家刚达到要求等级
     */
    public void clearOnlineReward(byte type){
        if(!isCanOnlineReward()) {
            return;
        }
        //等级刚达到，初始化倒计时
        if (type ==0){
            FunctionConfig functionConfig = DataFactory.getInstance().getGameObject(DataFactory.FUNCTION_KEY, FunctionFactory.ONLINE_REWARD);
            if(functionConfig == null){
               return;
            }
            if (functionConfig.lv !=player.getLv()){
                return;
            }
            this.beginTime =DataFactory.currentTime;

        }else {
            this.beginTime = 0;
        }
        this.onlineRewardList = new ArrayList<>();
        this.AllOnlineTime = 0;
    }

    /**
     * 是否可以领取
     * @param onlineId
     * @return
     */
    public boolean setOnlineReWardState(int onlineId){
        LogUtils.info("已領取的onlineRewardList：" + onlineRewardList.toString());
        if(!isCanOnlineReward()) {
            return false;
        }
        //今日已领取该档奖励
        if(onlineRewardList.contains(onlineId)){
            return false;
        }
        //累计时间
        OnlineRewardConfig config =DataFactory.getInstance().getGameObject(DataFactory.ONLINE_REWARD,onlineId);
        if (this.AllOnlineTime>=config.totalTime*1000){
            onlineRewardList.add(onlineId);
            LogUtils.info("领取成功：onlineRewardList：" + onlineRewardList.toString());
            return true;
        }
        return false;
    }

    /**
     * 累加在线时间
     * @return
     */
    public void updateOnlineRewardTime(){
        if(!isCanOnlineReward()) {
            return;
        }
        //开始计算累加的时间
        long lastTime =DataFactory.currentTime;
        if(lastTime<=this.beginTime || this.beginTime == 0){
            return;
        }
        //计算累加时间时查看是否已经到了下一天
        if (beginTime!=0 &&!TimeUtils.isToday(beginTime)){
            clearOnlineReward((byte) 1);
        }else {
            long diffTime = lastTime - this.beginTime;
            this.AllOnlineTime = this.AllOnlineTime + diffTime;
            this.beginTime = lastTime;
        }
        LogUtils.info("******************AllOnlineTime：" + AllOnlineTime + "lastTime:"+lastTime);
    }

    /**
     * 玩家登陆设置开始累计时间
     */
    public void setBeginTime(){
        if(!isCanOnlineReward()) {
            return;
        }
        //登陆时判断是否跟上次下线是同一天
        if (beginTime!=0 &&!TimeUtils.isToday(beginTime)){
            clearOnlineReward((byte) 1);
        }
        LogUtils.info("setBeginTime before--AllOnlineTime：" + AllOnlineTime + ",beginTime：" + beginTime);
        this.beginTime =DataFactory.currentTime;
        LogUtils.info("setBeginTime after--AllOnlineTime：" + AllOnlineTime + ",beginTime：" + beginTime);
    }

    /**
     * 等级是否满足
     * @return
     */
    public boolean isCanOnlineReward(){
        FunctionConfig functionConfig = DataFactory.getInstance().getGameObject(DataFactory.FUNCTION_KEY, FunctionFactory.ONLINE_REWARD);
        if(functionConfig != null){
            return player.getLv()>=functionConfig.lv;
        }
           return true;
    }

    /**
     * 是否有可领取的奖励
     */
    public boolean isGetAllReward()
    {
//        LogUtils.info("已领取的奖励：onlineRewardList：" + onlineRewardList.toString());
        List<OnlineRewardConfig> list = DataFactory.getInstance().getDataList(DataFactory.ONLINE_REWARD);
        for (OnlineRewardConfig config:list){
            if (this.AllOnlineTime>=config.totalTime * 1000 &&!this.onlineRewardList.contains(config.id)){
                LogUtils.info("checkRedPoint 有可领取的奖励 红点提示：" +config.id +"time:"+config.totalTime +"-AllOnlineTime");
                return true;
            }
        }
        return false;
    }

    @Override
    public void init() {
        clearOnlineReward((byte) 1);
    }

    @Override
    public void checkInit() {
        if (beginTime<=0)
            init();
    }

    @Override
    public void reset() {
    }

    @Override
    public void checkReset() {
    }

    @Override
    public void writeTo(ByteBuffer buffer) {
        updateOnlineRewardTime();
        buffer.writeByte(onlineRewardList.size());
        onlineRewardList.forEach(buffer::writeInt);
        buffer.writeLong(beginTime);
        buffer.writeLong(AllOnlineTime);
    }

    @Override
    public void loadFrom(ByteBuffer buffer) {
        int size = buffer.readByte();
        this.onlineRewardList = new ArrayList<>();
        for (int i = 0; i < size; i ++){
            onlineRewardList.add(buffer.readInt());
        }
        beginTime =buffer.readLong();
        AllOnlineTime =buffer.readLong();
    }

    public ArrayList<Integer> getOnlineRewardList() {
        return onlineRewardList;
    }

    public long getAllOnlineTime() {
        //更新在线时间
        updateOnlineRewardTime();
        return AllOnlineTime;
    }

}
